﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using POSPlus.PrintManager;

namespace EZBarTender_Wrapper.EDLP
{
	public partial class EdlpPrinter : Form
	{
		private string _connectionString;

		public string ConnectionString
		{
			get { return _connectionString; }
		}

		public EdlpPrinter(string connectionString)
		{
			InitializeComponent();
			_connectionString = connectionString;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			DialogResult result = (new TearListPrintPreview(ConnectionString)).ShowDialog();
		}

		private void EnableButtons(bool enable)
		{
			ui_addAllToPrintManager.Enabled = enable;
			ui_addNewToPrintManager.Enabled = enable;
			button2.Enabled = enable;
		}

		private void ui_addAllToPrintManager_Click(object sender, EventArgs e)
		{
			TagTypeDialog dialog = new TagTypeDialog();
			DialogResult result = dialog.ShowDialog();
			if (result == DialogResult.OK)
			{
				EnableButtons(false);
				backgroundWorker.RunWorkerAsync(new object[] {"All", dialog.Type});
			}
		}

		private void ui_addNewToPrintManager_Click(object sender, EventArgs e)
		{
			TagTypeDialog dialog = new TagTypeDialog();
			DialogResult result = dialog.ShowDialog();
			if (result == DialogResult.OK)
			{
				EnableButtons(false);
				backgroundWorker.RunWorkerAsync(new object[] { "New", dialog.Type });
			}
		}

		private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
		{
			object[] args = (object[])e.Argument;

			switch ((string)args[0])
			{
				case "All":
					DataTable results = EdlpInfo.GetAllEdlp(ConnectionString);
					foreach (DataRow row in results.Rows)
					{
						PrintManagerDisplay display = new PrintManagerDisplay(ConnectionString);
						display.Upc = (string)row["Upc"];
						if (DatabaseObject.FromDB<string>(row["ShelfTagType"]) == "SM")
						{
							display.JobName = "All Edlp Small Labels";
							if ((string)args[1] == "6UPSB")
								display.Type = "12UPSB";
							else
								display.Type = (string)args[1];
						}
						else
						{
							display.JobName = "All Edlp";
							display.Type = (string)args[1];
						}
						display.LocAisle = DatabaseObject.FromDB<int>(row["LocAisle"]);
						display.LocBlock = DatabaseObject.FromDB<int>(row["LocBlock"]);
						display.LocSeq = DatabaseObject.FromDB<int>(row["LocSeq"]);
						if (!display.Update())
							display.Create();
					}
					break;
				case "New":
					results = EdlpInfo.GetNewEdlp(ConnectionString);
					foreach (DataRow row in results.Rows)
					{
						PrintManagerDisplay display = new PrintManagerDisplay(ConnectionString);
						display.Upc = (string)row["Upc"];
						if (DatabaseObject.FromDB<string>(row["ShelfTagType"]) == "SM")
						{
							display.JobName = "All Edlp Small Labels";
							if ((string)args[1] == "6UPSB")
								display.Type = "12UPSB";
							else
								display.Type = (string)args[1];
						}
						else
						{
							display.JobName = "All Edlp";
							display.Type = (string)args[1];
						}
						display.LocAisle = DatabaseObject.FromDB<int>(row["LocAisle"]);
						display.LocBlock = DatabaseObject.FromDB<int>(row["LocBlock"]);
						display.LocSeq = DatabaseObject.FromDB<int>(row["LocSeq"]);
						if (!display.Update())
							display.Create();
					}
					break;
			}

		}

		private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			EnableButtons(true);
			if (OnJobAdded != null)
				OnJobAdded();
		}

		public delegate void JobAdded();
		public event JobAdded OnJobAdded;
	}
}